package com.bluno.view;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import com.bluno.Util;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class Resourcer {
	/**
	 * Manager do diretorio asset.
	 */
	public static AssetManager asset;
	public static Map<String,Bitmap> imageMap;
	
	public static void loadCardsImage()
	{
		if(imageMap == null)
			imageMap = new HashMap<String,Bitmap>();
		
		
		
		Bitmap deck =  loadBitmap("deck.png");
		int w = 56;
		int h = 90;
		//Carrega vermelhas
		for(int i = 0; i < 10; ++i)
		{
			imageMap.put(i+"R",  Bitmap.createBitmap(deck, i*w, 0, w, h));
		}
		imageMap.put("TR",  Bitmap.createBitmap(deck, 10*w, 0, w, h));
		imageMap.put("SR",  Bitmap.createBitmap(deck, 11*w, 0, w, h));
		imageMap.put("IR",  Bitmap.createBitmap(deck, 12*w, 0, w, h));

		for(int i = 0; i < 10; ++i)
		{
			imageMap.put(i+"G",  Bitmap.createBitmap(deck, i*w, h, w, h));
		}
		imageMap.put("TG",  Bitmap.createBitmap(deck, 10*w, h, w, h));
		imageMap.put("SG",  Bitmap.createBitmap(deck, 11*w, h, w, h));
		imageMap.put("IG",  Bitmap.createBitmap(deck, 12*w, h, w, h));
		
		for(int i = 0; i < 10; ++i)
		{
			imageMap.put(i+"Y",  Bitmap.createBitmap(deck, i*w, h*2, w, h));
		}
		imageMap.put("TY",  Bitmap.createBitmap(deck, 10*w, h*2, w, h));
		imageMap.put("SY",  Bitmap.createBitmap(deck, 11*w, h*2, w, h));
		imageMap.put("IY",  Bitmap.createBitmap(deck, 12*w, h*2, w, h));
		
		for(int i = 0; i < 10; ++i)
		{
			imageMap.put(i+"B",  Bitmap.createBitmap(deck, i*w, h*3, w, h));
		}
		imageMap.put("TB",  Bitmap.createBitmap(deck, 10*w, h*3, w, h));
		imageMap.put("SB",  Bitmap.createBitmap(deck, 11*w, h*3, w, h));
		imageMap.put("IB",  Bitmap.createBitmap(deck, 12*w, h*3, w, h));
		

		imageMap.put("FR",  Bitmap.createBitmap(deck, 13*w, 0, w, h));
		imageMap.put("FG",  Bitmap.createBitmap(deck, 13*w, h, w, h));
		imageMap.put("FY",  Bitmap.createBitmap(deck, 13*w, h*2, w, h));
		imageMap.put("FB",  Bitmap.createBitmap(deck, 13*w, h*3, w, h));
		

		imageMap.put("LR",  Bitmap.createBitmap(deck, 14*w, 0, w, h));
		imageMap.put("LG",  Bitmap.createBitmap(deck, 14*w, h, w, h));
		imageMap.put("LY",  Bitmap.createBitmap(deck, 14*w, h*2, w, h));
		imageMap.put("LB",  Bitmap.createBitmap(deck, 14*w, h*3, w, h));
		
		imageMap.put("WL",  Bitmap.createBitmap(deck, 15*w, 0, w, h));
		imageMap.put("WF",  Bitmap.createBitmap(deck, 15*w, h, w, h));
		
		
		
		deck.recycle();

		Bitmap faceDown =  loadBitmap("face_down0.png");
		imageMap.put("facedDownImage0", faceDown);
		faceDown =  loadBitmap("face_down1.png");
		imageMap.put("facedDownImage1", faceDown);
		
	}
	public static void loadGUIimages()
	{
		if(imageMap == null)
			imageMap = new HashMap<String,Bitmap>();
		
		 Bitmap background = loadBitmap("background0.png");		 
		 imageMap.put("background0", Bitmap.createScaledBitmap(background, Util.SCREEN_WIDTH,
				 Util.SCREEN_HEIGHT, true));
		 background.recycle();
		 Bitmap playButton = loadBitmap("new_game_button.png");
		 imageMap.put("newGameButton0", Bitmap.createBitmap(playButton, 0, 0, 285, 75));
		 imageMap.put("newGameButton1", Bitmap.createBitmap(playButton, 0, 75, 285, 75));
		 playButton = loadBitmap("connect_button.png");
		 imageMap.put("connectButton0", Bitmap.createBitmap(playButton, 0, 0, 285, 75));
		 imageMap.put("connectButton1", Bitmap.createBitmap(playButton, 0, 75, 285, 75));
		 playButton.recycle();
		 
		 Bitmap drawButton = loadBitmap("draw_button.png");
		 imageMap.put("drawButton0", Bitmap.createBitmap(drawButton, 0, 0, 80, 48));
		 imageMap.put("drawButton1", Bitmap.createBitmap(drawButton, 0, 48, 80, 48));	
		 drawButton.recycle();
		 Bitmap logo = loadBitmap("logo.png");
		 imageMap.put("logo", logo);
		 
		 Bitmap selectColor = loadBitmap("selectColor.png");
		 imageMap.put("selectColor", selectColor);
	}
	/**
	 * Abre um arquivo imagem e retorna o objeto bitmap.
	 * @param src
	 * @return
	 */
	private static Bitmap loadBitmap(String src)
	{
		Bitmap bmp = null;
		
			try
			{
				InputStream in = asset.open(src);
				BufferedInputStream buf = new BufferedInputStream(in);
				bmp = BitmapFactory.decodeStream(buf);
				
			}
			catch(Exception e)
			{
				Log.e("Error reading file", e.toString());
			}
			
		return bmp;
	}
}
